<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="讲师">
        <el-select
          v-model="queryParams.teacherId"
          placeholder="请选择讲师"
          clearable
        >
          <el-option
            v-for="item in teacherRole"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班主任">
        <el-select
          v-model="queryParams.headmasterId"
          placeholder="请选择班主任"
          clearable
        >
          <el-option
            v-for="item in headmasterRole"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级类型">
        <el-select
          v-model="queryParams.classType"
          placeholder="请选择班级类型"
          clearable
        >
          <el-option
            v-for="item in dict.type.taa_infclass_classType"
            :key="item.value"
            :label="item.label"
            :value="item.value * 1"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开班日期">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开班日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结课日期">
        <el-date-picker
          clearable
          v-model="queryParams.finishTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结课日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['taa:infclass:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['taa:infclass:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['taa:infclass:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="infclassList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="班级封面" align="center" width="100">
        <template slot-scope="scope">
          <image-preview
            :src="baseUrl + scope.row.classCover"
            :width="100"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="讲师" align="center" prop="teacherName" />
      <el-table-column label="班主任" align="center" prop="headmasterName" />
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="班级类型" align="center" prop="classTypeName" />
      <el-table-column label="开班日期" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结课日期" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否结课" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.finish == 0 ? "否" : "是" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click.native="GoToClaDetails(scope.row)"
            >详情
            <!-- <router-link @click.native="GoToClaDetails(scope.row)">详情</router-link> -->
            <!-- <router-link to="/claDetails">详情</router-link> -->
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['taa:infclass:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['taa:infclass:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班级信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班级封面">
          <image-upload v-model="form.classCover" />
        </el-form-item>
        <el-form-item label="讲师" prop="teacherId">
          <el-select
            v-model="form.teacherId"
            placeholder="请选择讲师"
            clearable
          >
            <el-option
              v-for="item in teacherRole"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班主任" prop="headmasterId">
          <el-select
            v-model="form.headmasterId"
            placeholder="请选择班主任"
            clearable
          >
            <el-option
              v-for="item in headmasterRole"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级名称" prop="className" style="width: 300px">
          <el-input v-model="form.className" placeholder="请输入班级名" />
        </el-form-item>
        <el-form-item label="班级类型" prop="classType">
          <el-select
            v-model="form.classType"
            placeholder="请选择班级类型"
            clearable
          >
            <el-option
              v-for="item in dict.type.taa_infclass_classType"
              :key="item.value"
              :label="item.label"
              :value="item.value * 1"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开班日期" prop="startTime">
          <el-date-picker
            clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开班日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结课日期">
          <el-date-picker
            clearable
            v-model="form.finishTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结课日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否结课" prop="finish">
          <el-radio v-model="form.finish" label="1">是</el-radio>
          <el-radio v-model="form.finish" label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            v-model="form.remark"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfclass,
  delInfclass,
  addInfclass,
  updateInfclass,
  getrole,
} from "@/api/taa/infclass";
export default {
  name: "Infclass",
  dicts: ["taa_infclass_classType"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班级信息表格数据
      infclassList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // classCover: null,
        teacherId: null,
        headmasterId: null,
        // teacherName: null,
        // headmasterName: null,
        // className: null,
        classType: null,
        startTime: null,
        // finishTime: null,
        // finish: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classCover: [
          { required: true, message: "班级封面不能为空", trigger: "blur" },
        ],
        teacherId: [
          { required: true, message: "讲师不能为空", trigger: "blur" },
        ],
        headmasterId: [
          { required: true, message: "班主任不能为空", trigger: "blur" },
        ],
        className: [
          { required: true, message: "班级名称不能为空", trigger: "blur" },
        ],
        classType: [
          { required: true, message: "班级类型不能为空", trigger: "change" },
        ],
        startTime: [
          { required: true, message: "开班日期不能为空", trigger: "blur" },
        ],
      },
      baseUrl: "http://mashang.eicp.vip:5555" + process.env.VUE_APP_BASE_API,
      teacher: {
        pageNum: 1,
        pageSize: 100,
        roleId: 100,
      },
      headmaster: {
        pageNum: 1,
        pageSize: 100,
        roleId: 101,
      },
      teacherRole: [],
      headmasterRole: [],
    };
  },
  created() {
    this.getList();
    this.getRoleInf();
  },
  methods: {
    // 获取教师和班主任角色信息
    getRoleInf() {
      getrole(this.teacher).then((response) => {
        this.teacherRole = response.rows;
      });
      getrole(this.headmaster).then((response) => {
        this.headmasterRole = response.rows;
      });
    },
    /** 查询班级信息列表 */
    getList() {
      this.loading = true;
      listInfclass(this.queryParams).then((response) => {
        this.infclassList = response.rows;
        this.total = response.total;
        console.log(response);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        classId: null,
        classCover: null,
        teacherId: null,
        headmasterId: null,
        teacherName: null,
        headmasterName: null,
        className: null,
        classType: null,
        classTypeName: null,
        startTime: null,
        finishTime: null,
        finish: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryParams");
      this.queryParams = this.$options.data().queryParams;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.classId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班级信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log('eee',row);
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = "修改班级信息";
      if (row.classType * 1 == 1) {
        this.form.classType = 1;
      } else if (row.classType * 1 == 2) {
        this.form.classType = 2;
      } else {
        this.form.classType = 3;
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.classId != null) {
            updateInfclass(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
            // console.log("this.form", this.form);
          } else {
            addInfclass(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.classId || this.ids;
      this.$modal
        .confirm('是否确认删除班级信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInfclass(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "taa/infclass/export",
        {
          ...this.queryParams,
        },
        `infclass_${new Date().getTime()}.xlsx`
      );
    },
    // 跳转页面
    GoToClaDetails(row) {
      // localStorage.removeItem("row");
      this.$router.push({
        path: "/claDetails",
        query: {
          row: row,
        },
      });
      // localStorage.setItem("row", row);
    },
  },
};
</script>
